package com.shycloud.mido.member.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shycloud.mido.member.api.entity.OrgStudentDetailed;
import com.shycloud.mido.member.config.PropertiesConfig;
import com.shycloud.mido.member.mapper.OrgStudentDetailedMapper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author Xiaoxing Yu
 * @date 2020/12/29 下午3:58
 **/
@Slf4j
public class AssignLimitUpdateJob implements Job {

	@Autowired
	private OrgStudentDetailedMapper studentDetailedMapper;

	@Autowired
	private PropertiesConfig propertiesConfig;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		if (propertiesConfig.getPeriodAssignThreshold() == null) return;

		List<OrgStudentDetailed> entityList = studentDetailedMapper.selectList(new QueryWrapper<OrgStudentDetailed>());

		for (OrgStudentDetailed entity: entityList) {
			if (entity.getCurAssginBal() == null || entity.getNextAssginBal() == null) {
				log.info("作业条数限制更新失败, studentId: " + entity.getId());
				continue;
			}

			entity.setCurAssginBal(entity.getNextAssginBal());
			entity.setNextAssginBal(propertiesConfig.getPeriodAssignThreshold());
			if (studentDetailedMapper.updateById(entity) != 1) {
				log.info("作业条数限制更新失败, studentId: " + entity.getId());
			}
		}
	}

}
